<?php
namespace App\Http\Controllers\Msg;

use App\Http\Controllers\Controller;
use App\Libraries\Decorator\Image\CoverDecorator;
use App\Libraries\Decorator\Tool\IsEndDecorator;
use App\Models\Msg\MsgComment;
use App\Models\Msg\MsgPraise;
use App\Models\Msg\MsgSystem;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Validator;


class MsgController extends Controller{

    protected $isEndDecorator;
    protected $coverDecorator;

    public function __construct(IsEndDecorator $isEndDecorator, CoverDecorator $coverDecorator){
        $this->isEndDecorator = $isEndDecorator;
        $this->coverDecorator = $coverDecorator;
    }

    /**
     * 消息统计
     * @param MsgComment $msgComment
     * @param MsgPraise $msgPraise
     * @return \Illuminate\Http\JsonResponse
     */
    public function sta(MsgComment $msgComment, MsgPraise $msgPraise){
        $data = [];
        $user_id = Auth::id();
        $data['at'] = 0;
        $data['system'] = 0;
        $data['comment'] = $msgComment->where('to_id', $user_id)->where('status', 0)->count();
        $data['praise'] = $msgPraise->where('to_id', $user_id)->where('status', 0)->count();
        return $this->sendSuccessResponse($data);
    }

    /**
     * 评论消息列表
     * @param MsgComment $msgComment
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function comment(MsgComment $msgComment, Request $request){
        $validator = Validator::make($request->all(), [
            'page' => 'required|integer',
            'size' => 'required|integer',
        ], [
            'page.required' => '10001',
            'page.integer' => '10002',
            'size.required' => '10001',
            'size.integer' => '10002',
        ]);
        if ($validator->fails()) {
            return $this->sendFailResponse(400, $validator->errors()->first());
        }

        $msgComment->addDecorator($this->isEndDecorator);
        $msgComment->addDecorator($this->coverDecorator);
        $data = $msgComment->getList(Auth::id(), $request);
        return $this->sendSuccessResponse($data);
    }

    /**
     * 点赞消息列表
     * @param MsgPraise $msgPraise
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function praise(MsgPraise $msgPraise, Request $request){
        $validator = Validator::make($request->all(), [
            'page' => 'required|integer',
            'size' => 'required|integer',
        ], [
            'page.required' => '10001',
            'page.integer' => '10002',
            'size.required' => '10001',
            'size.integer' => '10002',
        ]);
        if ($validator->fails()) {
            return $this->sendFailResponse(400, $validator->errors()->first());
        }
        $msgPraise->addDecorator($this->isEndDecorator);
        $msgPraise->addDecorator($this->coverDecorator);
        $data = $msgPraise->getList(Auth::id(), $request);
        return $this->sendSuccessResponse($data);
    }

    /**
     * 系统消息列表
     * @param MsgSystem $msgSystem
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function system(MsgSystem $msgSystem, Request $request){
        $validator = Validator::make($request->all(), [
            'page' => 'required|integer',
            'size' => 'required|integer',
        ], [
            'page.required' => '10001',
            'page.integer' => '10002',
            'size.required' => '10001',
            'size.integer' => '10002',
        ]);
        if ($validator->fails()) {
            return $this->sendFailResponse(400, $validator->errors()->first());
        }
        $msgSystem->addDecorator($this->isEndDecorator);
        $msgSystem->addDecorator($this->coverDecorator);
        $data = $msgSystem->getList(Auth::id(), $request);
        return $this->sendSuccessResponse($data);
    }
}